var api = require('../../../../config/api.js');
var util = require('../../../../utils/util.js');
var app = getApp();
Page({
  data: {
    name: '',
    mobile: '',
    code: '',
    nickname: '',
    status: 0,
    mobileLength: false,
    mobileFormat: false,
    mobileError: ''
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成
    let that = this;
    util.request(api.Getshangjifenxiaoshang).then(function (res) {
      if (res.result == 1) {
        that.setData({
          nickname: res.data.nickname,
          name: res.data.fxsname,
          mobile: res.data.fxsmobile,
          code: res.data.fxscode,
          status: res.data.fxsstatus
        })
      }
    });
  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  fenxiaoshangshenqing: function () {
    var that = this;
    if (this.data.name.length < 1 || this.data.mobile.length < 1) {
      wx.showModal({
        title: '错误信息',
        content: '请输入分销商名称和手机号',
        showCancel: false
      });
      return false;
    }
    const regex = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/
    if (that.data.mobile.length !== 0 && that.data.mobile.length !== 11) {
      that.setData({
        mobileLength: true,
        mobileError: '手机长度有误'
      })
      return false;
    } else if (that.data.mobile.length !== 0 && !regex.test(that.data.mobile)) {
      that.setData({
        mobileFormat: true,
        mobileError: '手机号有误'
      })
      return false;
    } else {
      that.setData({
        mobileLength: false,
        mobileFormat: false,
        mobileError: ''
      })
    }
    util.request(api.Fenxiaozhongxinshenqing, {
      name: that.data.name,
      mobile: that.data.mobile
    }).then(function (res) {
      if (res.result == 1) {
        if (res.data.fenxiaoshangstauts == 0) {
          wx.redirectTo({
            url: "/pages/ucenter/fenxiao/fenxiao/fenxiao"
          });
        }
        if (res.data.fenxiaoshangstauts == 2) {
          that.setData({
            status: 2
          })
        }
      }
    });
  },
  bindNameInput: function (e) {
    this.setData({
      name: e.detail.value
    });
  },
  /**
   * 输入手机号
   */
  bindMobileInput: function (event) {
    this.setData({
      mobile: event.detail.value
    })
  },
  //获取电话号码
  getPhoneNumber: function (e) {
    let that = this;
    if (e.detail.errMsg !== "getPhoneNumber:ok") {
      // 拒绝授权
      return;
    }
    var iv = e.detail.iv;
    var encryptedData = e.detail.encryptedData;
    var appid = app.globalData.appid;
    wx.login({
      success(res) {
        var code = res.code
        util.request(api.GetSessionkey, {
          code: code,
          appid: appid
        }, 'POST').then(function (res) {
          if (res.result == 1) {
            util.request(api.AuthBindPhone, {
              iv: iv,
              encryptedData: encryptedData,
              sessionKey: res.data.session_key
            }).then(function (res) {
              if (res.result == 1) {
                that.setData({
                  mobile: res.data.phone //设置手机号码
                });
              } else {
                wx.showToast({
                  title: '获取信息失败',
                  icon: 'none'
                })
              }
            }).catch((err) => {
              wx.showToast({
                title: '获取信息失败',
                icon: 'fail'
              })
            });;
          }
        });
      }
    })
  },
})